<%@page import="systems.ConfigSystem"%>
<%@page import="entities.SiteConfig"%>
<%@page import="systems.AdsSystem"%>
<%@page import="java.util.TreeMap"%>
<%@page import="entities.MetaSeo"%>
<%@page import="helpers.StringHelper"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="helpers.DateHelper"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="helpers.IdUniqueHelper"%>
<%@page import="models.QuestionModel"%>
<%@page import="com.google.gson.Gson"%>
<%@page import="entities.Answer"%>
<%@page import="entities.Category"%>
<%@page import="java.util.Map"%>
<%@page import="entities.Author"%>
<%@page import="entities.Question"%>
<%@page import="java.util.List"%>
<%
SiteConfig siteConfig = ConfigSystem.getConfigs();
Question question = (Question) request.getAttribute("question");
Map<String, Author> authors = (Map<String, Author>) request.getAttribute("authors");
Category category = (Category) request.getAttribute("category");
List<Answer> answers = (List<Answer>) request.getAttribute("answers");
Author author = null;
MetaSeo metaseo = (MetaSeo) request.getAttribute("metaseo");

//Set content for Seo
Map<String, String> replates = new TreeMap<String, String>();
replates.put("__SERVER_NAME__", UrlHelper.getDomainServer(request));
replates.put("__SERVER_URL__", UrlHelper.getServerUrl(request));
replates.put("__QUESTION_ID__", question.getId());
replates.put("__QUESTION_TITLE__", question.getTitle());
replates.put("__QUESTION_CONTENT__", StringHelper.html2text(question.getContent()));

String title = StringHelper.replateContent(metaseo.getTitle(), replates);
String description = StringHelper.replateContent(metaseo.getDescription(), replates);
String keywords = StringHelper.replateContent(metaseo.getKeywords(), replates);
String url = StringHelper.replateContent(metaseo.getOgurl(), replates);
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/templates/default_lang/layouts/header.jsp">
	<jsp:param name="title" value="<%=title %>" />
    <jsp:param name="description" value="<%=description %>" />
    <jsp:param name="keywords" value="<%=keywords %>" />
    <jsp:param name="ogtitle" value="<%=title %>" />
    <jsp:param name="ogurl" value="<%=url %>" />
    <jsp:param name="ogdescription" value="<%=description%>" />
</jsp:include>
	<script type="text/javascript" src="/templates/<%=siteConfig.getTemplate() %>/js/question_info.js"></script>
	
		<div class="mainContent clearafter">
			<div class="leftMainContent">
			
				<!-- Content Tab -->
				<div class="content">
					<div class="content_in">
						<!-- post -->
						<div class="posts">
							<!-- question_item -->
							<div class="question_item clearafter">
								<span class="total_answers">
									<a href="javascript: return false;"><%=question.getAnswers().size() %></a> Answers
								</span>
								<h1>
									<%=question.getTitle() %>
								</h1>
								<p>
									<%
									if (!question.getAuthor().isEmpty()) {
										author = authors.get(question.getAuthor());
										if (author != null) {
									%>
									<span class="user">
										Asked by: 
										<strong>
											<a href="/author/<%=author.getAlias() %>-<%=author.getId() %>" title="<%=author.getName() %>"><%=author.getName() %></a>
										</strong>
									</span>
									<% 	}
									} %>
									<% if (category != null) { %>
									<span class="pcate">
										<a rel="tag" href="/category/<%=category.getAlias()%>-<%=category.getId() %>"><%=category.getName() %></a><br>
									</span>
									<% } %>
									<span class="adv">
										<%=AdsSystem.getAds468x60() %>
									</span>
								</p>
							</div>
							<!-- end question_item -->
							<p>
							<span class="advContent right">
								<%=AdsSystem.getAds200x200() %>
							</span>
							<%=question.getContent() %>
							</p>
						</div>
						<!-- end post -->
						
						<!-- comments -->
						<div id="comments">
							<div class="comments_in">
								<h3><%=answers.size() %> Answers </h3>
								
								<br />
								
								<ol class="commentlist">
									<%
									for (Answer answer : answers) {
										author = authors.get(answer.getAuthor());
									%>
									<li class="comment_item clearafter">
										<div class="meta left">
											<% if (author != null) { %>
											<a href="/author/<%=author.getAlias() %>-<%=author.getId() %>" title="<%=author.getName() %>">
												<img src="<%=author != null? author.getAvatar() : "" %>" width="48" height="48">
											</a>
											<% } else { %>
											<img src="<%=answer.getAuthorAvatar() %>" width="48" height="48">
											<% } %>
										</div>
										<div class="answer left">
											<p class="author">
												<span class="author_name">
													<% if (author != null) { %>
													<strong><a href="/author/<%=author.getAlias() %>-<%=author.getId() %>" title="<%=author.getName() %>"><%=author != null? author.getName() : "" %></a></strong>
													<% } else { %>
													<strong><%=answer.getAuthorName() %></strong>
													<% } %>
												</span>
												<small> on <%=DateHelper.getStringFromDate(answer.getCreated(), "MMM dd, yyyy") %> </small>
											</p>
											<p><%=answer.getContent() %></p>
										</div>
									</li>
									<% } %>
								</ol>
							</div>
						</div>
						<!-- end comments -->
						
						
						<!-- facebook comment -->
						<div id="fb-root"></div>
						<script src="http://connect.facebook.net/en_US/all.js#appId=165471683512060&amp;xfbml=1"></script>
						<fb:comments href="<%=UrlHelper.getServerUrl(request) %>/question/<%=question.getId() %>" width="660"></fb:comments>
						<!-- end facebook comment -->
						
						
						<!-- Same Question -->
						<% if (category != null) { %>
						<div class="other_question">
							<h3>Other Question in <%=category.getName() %></h3>
							<%
							if (!question.getLinks().isEmpty()) {
							%>
							<ul>
								<%
									QuestionModel questionModel = QuestionModel.getInstance();
									Map<String, String> links = (Map<String, String>) new Gson().fromJson(question.getLinks(), Map.class);
									for (Map.Entry<String, String> entry : links.entrySet()) {
										//Check key question exist
										url = entry.getValue();
										
								%>
									<li><a href="/question/<%=URLEncoder.encode(entry.getValue()) %> "><%=entry.getKey() %></a></li>
								<% } %>
							</ul>
							<% } %>
						</div>
						<% } %>
						<!-- end Same Question  -->
					</div>
				</div>
				<!-- End Content tab -->
			</div>
			<jsp:include page="/templates/default_lang/elements/right.jsp" />
		</div>
<jsp:include page="/templates/default_lang/layouts/footer.jsp" />